/**
 * Copyright 2010 Philippe Beaudoin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.philbeaudoin.gwtpsample.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.dispatch.client.DispatchAsync;
import com.philbeaudoin.gwtp.mvp.client.EventBus;
import com.philbeaudoin.gwtp.mvp.client.PresenterImpl;
import com.philbeaudoin.gwtp.mvp.client.View;
import com.philbeaudoin.gwtp.mvp.client.proxy.Place;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceManager;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceRequest;
import com.philbeaudoin.gwtp.mvp.client.proxy.Proxy;
import com.philbeaudoin.gwtp.mvp.client.proxy.RevealRootContentEvent;
import com.philbeaudoin.gwtp.mvp.client.annotations.NameToken;
import com.philbeaudoin.gwtp.mvp.client.annotations.ProxyCodeSplit;
import com.philbeaudoin.gwtpsample.shared.SendTextToServer;
import com.philbeaudoin.gwtpsample.shared.SendTextToServerResult;

public class ResponsePresenter extends PresenterImpl<ResponsePresenter.MyView, ResponsePresenter.MyProxy> {

  public static final String nameToken = "response";
  public static final String textToServerParam = "textToServer";


  public interface MyView extends View {
    public Button getCloseButton();
    public void setTextToServer(String textToServer);
    public void setServerResponse(String serverResponse);
  }

  @ProxyCodeSplit
  @NameToken(nameToken)
  public interface MyProxy extends Proxy<ResponsePresenter>, Place {}

  private final PlaceManager placeManager;
  private final DispatchAsync dispatcher;

  private String textToServer = null;

  @Inject
  public ResponsePresenter(
      EventBus eventBus, MyView view, 
      MyProxy proxy, 
      PlaceManager placeManager,
      DispatchAsync dispatcher) {
    super(eventBus, view, proxy);
    this.placeManager  = placeManager;
    this.dispatcher = dispatcher;
  }

  @Override
  protected void revealInParent() {
    RevealRootContentEvent.fire( eventBus, this );
  }

  @Override
  protected void onBind() {
    super.onBind();
    registerHandler( getView().getCloseButton().addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        placeManager.revealPlace( new PlaceRequest(MainPagePresenter.nameToken) );
      }
    }) );
  }

  @Override
  protected void onReset() {
    super.onReset();
    getView().setTextToServer( textToServer );
    getView().setServerResponse( "Waiting for response..." );
    dispatcher.execute( 
        new SendTextToServer(textToServer), 
        new AsyncCallback<SendTextToServerResult>(){
          @Override
          public void onFailure(Throwable caught) {
            getView().setServerResponse( "An error occured: " + caught.getMessage() );
          }
          @Override
          public void onSuccess(SendTextToServerResult result) {
            getView().setServerResponse( result.getResponse() );
          }
        } );    
  }

  @Override
  public void prepareFromRequest(PlaceRequest request) {
    super.prepareFromRequest(request);
    textToServer  = request.getParameter(textToServerParam, null);
  }

  @Override
  public PlaceRequest prepareRequest(PlaceRequest request) {
    request = super.prepareRequest(request);
    if( textToServer != null )
      return request.with(textToServerParam, textToServer);
    return request;
  }
}
